package com.app.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.app.common.Result;
import com.app.model.Prize;
import com.app.service.IPrizeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 奖品 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2023-12-16
 */
@RestController
@RequestMapping("/prize")
public class PrizeController {
    @Autowired
    private IPrizeService iPrizeService;

    /**
     * 保存奖品
     *
     * @param prize 奖品
     * @return {@link Result}
     */
    @PostMapping("/admin/save")
    public Result savePrize(@RequestBody Prize prize) {
        Prize newPrize = new Prize();
        if (prize.getId() != null && prize.getId() > 0) {
            List<Prize> prizeList = prize.getPrizeList();
            QueryWrapper<Prize> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("game_id", prize.getGameId());
            Prize prizeServiceOne = iPrizeService.getOne(queryWrapper);
            List<Prize> list = JSON.parseObject(prizeServiceOne.getPrize(), new TypeReference<List<Prize>>() {
            });
            for (int i = 0; i < prizeList.size(); i++) {
                for (int i1 = 0; i1 < list.size(); i1++) {
                    if (StrUtil.isBlankIfStr(prizeList.get(i).getPid())) {
                        prizeList.get(i).setPid(IdUtil.nanoId(8));
                        prizeList.get(i).setGameId(prize.getGameId());
                    }
                    if (prizeList.get(i).getPid().equals(list.get(i1).getPid())) {
                        prizeList.get(i).setCurrentNum(list.get(i1).getCurrentNum());
                    }
                }
            }
            newPrize.setId(prizeServiceOne.getId());
            newPrize.setGameId(prizeServiceOne.getGameId());
            newPrize.setPrize(JSON.toJSONString(prizeList));
            return Result.success(this.iPrizeService.updateById(newPrize));
        } else {
            List<Prize> prizeList = prize.getPrizeList();
            if (prizeList != null) {
                for (int i = 0; i < prizeList.size(); i++) {

                    prizeList.get(i).setPid(IdUtil.nanoId(8));
                    prizeList.get(i).setGameId(prize.getGameId());
                }
                prize.setPrize(JSON.toJSONString(prizeList));
            }
            prize.setCreateTime(LocalDateTime.now());
            return Result.success(this.iPrizeService.save(prize));
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println(IdUtil.nanoId(8));
        }
    }
    /**
     * 奖品列表
     *
     * @return {@link Result}
     */
    @GetMapping("/admin/list")
    public Result prizeList(Integer gameId) {
        return Result.success(iPrizeService.prizeList(gameId));
    }

    /**
     * 奖品删除
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/admin/delete")
    public Result prizeDelete(Integer id) {
        return Result.success(this.iPrizeService.removeById(id));
    }

    /**
     * 奖品详情
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/admin/details")
    public Result prizeDetails(Integer id) {
        return Result.success(this.iPrizeService.getById(id));
    }

}

